/*****************webSocket start**********************/
function webSocketChat(clientId, clientName) {
    if (!window.webSocketObj) {
        /*var webSocketUrl = '192.168.1.221:8090/HospitalWebSocket/webSocketJoin';*/
        var webSocketUrl = '115.159.142.117/HospitalWebSocket/webSocketJoin';
        //判断当前浏览器是否支持WebSocket
        if ('WebSocket' in window) {
            if (location.protocol === "https:") {
                window.webSocketObj = new WebSocket("wss://" + webSocketUrl + "/" + clientId + "/" + clientName + "/" + parseInt(1) + "/" + 2);
            } else {
                window.webSocketObj = new WebSocket("ws://" + webSocketUrl + "/" + clientId + "/" + clientName + "/" + parseInt(1) + "/" + 2);
            }
        } else {
            alert('当前浏览器不支持 websocket');
        }
        //连接发生错误的回调方法
        webSocketObj.onerror = function () {
            try {
                setMessageInnerHTML("<p>连接失败，请重新加载</p>");
            } catch (e) {
                console.log(e);
            }
        };

        //连接成功建立的回调方法
        webSocketObj.onopen = function () {
            console.log("WebSocket连接成功");
        };

        //接收到消息的回调方法
        window.chatGroupIdList = {};
        webSocketObj.onmessage = function (event) {
            var messageObj = JSON.parse(event.data);
            if ($('.group').attr('group-id') == messageObj.groupId) {
                try {
                    setMessageInnerHTML(messageObj.html);
                } catch (e) {
                    console.log(e);
                }
            }

            var groupEl = $('#group-' + messageObj.groupId);
            if (!groupEl.length) {
                chatGroupIdList[messageObj.groupId] = Date.parse(new Date());
                queryNewList();
            } else {
                groupEl.addClass('blink');
                setTimeout(function () {
                    groupEl.removeClass('blink');
                }, 30 * 1000);
                var htmlStr = '<span>' + messageObj.contentPrefix + '发了' + (messageObj.content === "[图片]" ? '一张图片' : '一句话') + '</span>';
                groupEl.find('.chat-content').html(htmlStr + htmlStr).each(function (index) {
                    var obj = $(this);
                    var margin = 0;
                    var width = obj.width() / 2;
                    var showWidth = obj.parent().width();

                    clearInterval(timerChatList[messageObj.groupId.toString()]);
                    if (showWidth >= width) {
                        obj.find('span').eq(1).remove();
                        return;
                    }
                    function runInterval() {
                        timerChatList[messageObj.groupId.toString()] = setInterval(function () {
                            margin--;
                            obj.css('margin-left', margin);
                            if (margin <= (0 - width)) {
                                obj.css('margin-left', 0);
                                margin = 0;
                            }
                        }, 26);
                    }

                    runInterval();
                });
            }
        };

        //连接关闭的回调方法
        webSocketObj.onclose = function () {
            try {
                setMessageInnerHTML("<p>您已处于离线状态</p>");
            } catch (e) {
                console.log(e);
            }
        };

        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function () {
            closeWebSocket();
        };


        //关闭WebSocket连接
        function closeWebSocket() {
            webSocketObj.close();
        }
    }
}
/*****************webSocket end**********************/